//数据库模块
const mysql = require('mysql')
    //1.创建数据库配置
let pool = mysql.createPool({
    host: 'localhost',
    port: 3306,
    database: 'wangyouqipai',
    user: 'root',
    password: '',
    multipleStatements: true
})

const request = require('request');

// var baseUrl = 'http://120.79.173.65:8801';
var baseUrl = 'https://tsfunweb.game.wangyouqipai.com';
var options = {
    method: 'POST',
    url: baseUrl,
    headers: {
        secretkey: "B$@%#081rsiEf2Rx4XI0WPFAf2kSBEf2rsiE-.-",
    },
    json: true,
}

//封装查询方法
let query = function(sql, params) {
    return new Promise((resolve, reject) => {
        //得到数据库连接,也可以用reject(err)
        pool.getConnection(function(err, conn) {
            if (err) {
                reject(err)
            } else {
                conn.query(sql, params, (err, rows) => {
                    if (err) {
                        reject(err)
                    } else {
                        resolve(rows);
                    }
                    //释放连接
                    conn.release();
                })
            }
        })
    })
}

let getData = function(options) {
    return new Promise((resolve, reject) => {
        request(options, function(err, res) {
            if (err) {
                reject(err)
            } else {
                var result = res.body;
                if (result.errcode) {
                    console.log('接口返回了错误', result.errcode, result.errmsg);
                    resolve({ code: result.errcode, data: result.errmsg })
                } else {
                    console.log(result)
                    resolve({ code: 200, data: result })
                }
            }
            options.url = baseUrl;
            options.body = null;
        });
    })

};
//超级管理员登录接口
exports.login = function(manager) {
    let sql = 'select agentId,username,roomCard,roletype from agent where tel=? and password=? and roletype=1';
    let params = [manager.tel, manager.password];
    return query(sql, params);
};
//代理登录接口
exports.login1 = function(user) {
    let sql = 'select tel,agentId,roletype,roomCard from agent where status = 1 and tel=? and password=?';
    let params = [user.tel, user.password];
    return query(sql, params);
};
//给玩家充值房卡
exports.addRoomCard = function(params) {
    options.url += '/user/addroomcard';
    options.body = {
        uid: parseInt(params.uid),
        count: parseInt(params.addNumber),
    };
    console.log(options)
    return getData(options)

};
//获取玩家信息接口
exports.getPlayerInfo = function(params) {
    options.url += '/user/base';
    options.body = {
        uid: parseInt(params)
    };
    return getData(options)

};
//获取大厅信息
exports.getGameHallInfo = function(params) {
    options.url += '/hall/getsysteminfo';
    options.body = {};
    return getData(options)
};
//修改大厅信息
exports.UpdateGameHallInfo = function(params) {
    options.url += '/hall/setsysteminfo';
    options.body = {...params };
    return getData(options)
};
//获取用户反馈信息
exports.getFeedBacks = function(params) {
    options.url += '/hall/getfeedbacklist';
    options.body = { status: 1 }; //  0:已删除 1:正常
    return getData(options)
};
// 更换亲友圈号
exports.changeTeaHouse = function(params) {
    options.url += '/teahouse/changecode';
    options.body = {
        oldcode:parseInt(params.oldcode),
        newcode:parseInt(params.newcode),
    };
    return getData(options)
};

// ---------------------------用户操作--------------------------
exports.getAllUser = function(options) {
    options.page_num = options.page_num || 1; //默认页数
    options.limit = parseInt(options.limit) || 5; //每页多少条       
    let start = (options.page_num - 1) * options.limit;
    let params = [start, options.limit];
    let sql = 'select * from user limit ?,? ;select count(*) as total from user';
    return query(sql, params);
};
//===========网游棋牌======================
exports.getAllAgents = function() {
    let sql = 'select * from agent where status =1 and roletype != 1';
    return query(sql);
}
exports.getOneAgent = function(tel) {
        let sql = 'select * from agent where tel=?';
        return query(sql, tel);
    }
    //开通代理
exports.addAgent = function(agent) {
    let sql = 'insert into agent set ?';
    return query(sql, agent);
};
//超级管理员给代理充卡 代理房卡增加 超级管理员房卡减少
exports.editAgent = function(reqBody) {
    let sql = 'update agent set roomCard=(roomCard+?) where agentId=? ; update agent set roomCard=(roomCard-?) where agentId =?';
    let params = [reqBody.addNumber, reqBody.agentId, reqBody.addNumber, reqBody.managerId];
    return query(sql, params);
};
//添加一条充卡记录  并修改代理的房卡数量
exports.addCardRecord = function(record) {
    console.log('record==========>')
    console.log(record)
    let sql = 'insert into record1 set agentId=?,uid=?,addCardNumber=?; update agent set roomCard=(roomCard-?) where agentId =?';
    let params = [record.agentId, record.uid, record.addCardNumber, record.addCardNumber, record.agentId]
    return query(sql, params);
};
//添加给给代理充卡记录
exports.addCardForAgentRecord = function(record) {
    let sql = 'update agent set roomCard=(roomCard+?) where agentId=?;insert into record set agentId=?,managerId=?,addCardNumber=?; update agent set roomCard=(roomCard-?) where agentId =?';
    let params = [record.addCardNumber, record.agentId, record.agentId, record.managerId, record.addCardNumber, record.addCardNumber, record.managerId]
    return query(sql, params);
};
//根据id查询代理充卡的记录
exports.getRecordById = function(agentId) {
    let sql = 'SELECT * FROM record1 where agentId =?';
    return query(sql, agentId);
};
//根据id查询超级管理员充卡的记录
exports.getRecordByManagerId = function(managerId) {
    let sql = 'SELECT * FROM record where managerId =?';
    return query(sql, managerId);
}